<?php


namespace api\admin\controller;

use api\admin\model\OrderModel;
use cmf\controller\RestBaseController;
use think\facade\Db;
use think\Request;

class OrderController extends RestBaseController
{
    //获取商品信息
    public function getGoodsList(){
        //获取搜索字段
        $name = input('name');
        $page = input('page/d',1);
        $listRow = input('list_row/d',10);
        //查询商品
        $goods = Db::name('goods')
            ->with('navigation')
            ->order(['id' => 'DESC'])
            ->field('id,name,image,photos,status,gid')
            ->where('name','like','%'.$name.'%')
            ->page($page,$listRow)
            ->select();
        $this->success('商品列表',$goods);
    }
    //商品详情
    public function info(){
//        $arr=[
//            '颜色' => ['红色', '黑色','白色'],
//            '尺码' => ['M', 'L','XL'],
//        ];
//        halt(json_encode($arr, JSON_UNESCAPED_UNICODE));
        $id = input('id/d');
        if(!$id){
            $this->error('商品不存在');
        }
        $goods = Db::name('goods')->field('id,goods_list')->find($id);
        $goods['goods_list'] = json_decode($goods['goods_list']);
        $this->success('商品详情',$goods);
    }
    public function order(Request $request){
        $data = $request->post();
//        $info=[
//            ['颜色'=>'黑色','尺码'=>'M','价格'=>'10'],
//            ['颜色'=>'红色','尺码'=>'L','价格'=>'20']
//        ];
//        halt(json_encode($info,JSON_UNESCAPED_UNICODE));
        //查询商品
        $goods = Db::name('goods')->where('id',$data['goods_id'])->find();
        if(!$goods){
            $this->error('该商品不存在');
        }
        if($goods['status'] != 1){
            $this->error('该商品已下架无法购买');
        }
        $address = Db::name('delivery')->where('id',$data['address_id'])->find();
        if(!$address){
            $this->error('地址不存在');
        }
        $insert_data = [
            'user_id' => $request->user_id,
            'goods_name' => $goods['name'],
            'goods_info' => json_encode($data['goods_info']),
            'price' => $goods['money'],
            'num' => $data['num'],
            'total_price' => round($goods['money'] * $data['num'], 2),
            'trade_no' => date('YmdHis').mt_rand(1111,9999),
            'user_name' => $address['consignee'],
            'user_tel' => $address['mobile'],
            'user_address' => $address['area'].$address['address'],
            //'pay_type' => $data['pay_type'],
            'status' => 0,
            'remarks' => $data['remarks'],
            'create_time' => date('Y-m-d H:i:s'),
        ];
        $model = new OrderModel();
        if ($model->save($insert_data)) {
            return json(['code' => '1', 'message' => '订单创建成功', 'data' => [
                'payInfo'=>[
                    'order_id' => $model->id
                ]
            ]]);
        } else {
            return json(['code' => '0', 'message' => '订单创建失败']);
        }
    }
    protected function getGoodsInfo($goods,$spe){
        $goodsSpe = json_decode($goods['info'],true);
        foreach ($goodsSpe as $value){
            if($value['颜色'] == $spe['颜色'] && $value['尺码'] == $spe['尺码']){
                return $value;
            }
        }
    }
    private function getGoodsInfo2($goods,$buy)
    {
        $goodsSku = json_decode($goods['info'],true);
        //循环的商品的规格
        foreach($goodsSku as $sku){
            $hasSku = true;
            //购买的规格
            foreach ($buy as $attr => $val) {
                if($sku[$attr] != $val) {
                    $hasSku = false;
                }
            }
            if ($hasSku) {
                return $sku;
            }else {
                continue;
            }
        }
    }

}